﻿#include "BrowserTableHandlerPhysicsUniform.h"
#include "OperatorsInterface/OperAbstractPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyUniform.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include <QPushButton>
namespace GUI
{
    void BrowserTableHandlerPhysicsUniform::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Material") << tr("Thickness[m]")
            << tr("Color") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerPhysicsUniform::updateData()
    {
        if (_tableWidget == nullptr) return;
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysicsPropManager* mgr = phys->getPhysicsPropManager();
        if (mgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsPhysicsProp*> list = mgr->getPhysicsProps(
            Acoustics::FITKAcousticsAbsPhysicsProp::APPUniformPlat);
        //物理属性数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为均质板
            Acoustics::FITKPhyscisPropertyUniform* prop = dynamic_cast<Acoustics::FITKPhyscisPropertyUniform*>
                (list.at(i));
            if (prop == nullptr) continue;
            //数据ID
            const int id = prop->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(prop->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(prop->getMaterialName(), id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(prop->getThickness()), id));
            QTableWidgetItem *coloritem = new QTableWidgetItem("", id);
            float color[3] = { 0,0,0 };
            prop->getColor(color);
            QPushButton* button = new QPushButton(_tableWidget);
            this->SetPushButtonColor(button, color[0] * 255, color[1] * 255, color[2] * 255);
            _tableWidget->setCellWidget(i, 3, button);
            connect(button, &QPushButton::clicked, this, [=] {this->onEditItemColor(button, prop); });
            _tableWidget->setItem(i, 4, new QTableWidgetItem(prop->getDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractPhysics * BrowserTableHandlerPhysicsUniform::getPhysicsPropOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractPhysics>("OperPhysicsPropUniform");
    }

    bool BrowserTableHandlerPhysicsUniform::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //材料和厚度能批量
        if (colIndex < 1|| colIndex > 2) return false;

        return true;
    }

}
